import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
// import { Layout, getParentLayout } from '@/utils/routerHelper'
import { Layout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'
// 动态路由 后续开启
// import { setupLayouts } from 'virtual:generated-layouts'
// import generatedRoutes from 'virtual:generated-pages'
// const routes = generatedRoutes.map((route) => {
//   console.log(route)
//   return route?.meta?.layout != false ? setupLayouts([route])[0] : route
// })

const { t } = useI18n()
export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/index.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/index.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/dashboard',
    component: Layout,
    name: 'Dashboard',
    redirect: '/dashboard/index',
    meta: {},
    children: [
      {
        path: 'index',
        name: 'DashboardIndex',
        component: () => import('@/views/Dashboard/index.vue'),
        meta: {
          title: '首页',
          icon: 'ep:home-filled'
        }
      }
    ]
  },
  {
    path: '/record',
    component: Layout,
    redirect: '/record/product/list',
    name: 'Record',
    meta: {
      title: '数据',
      icon: 'charm:chart-line'
    },
    children: [
      {
        path: 'product',
        name: 'Product',
        redirect: '/record/product/list',
        component: () => import('@/views/Record/Product/List/index.vue'),
        meta: {
          title: '商品管理',
          icon: 'ci:shopping-cart-02',
          alwaysShow: true
        },
        children: [
          {
            path: 'list',
            name: 'ProductList',
            component: () => import('@/views/Record/Product/List/index.vue'),
            meta: {
              title: '商品列表'
            }
          },
          {
            path: 'classification',
            name: 'ProductClassification',
            component: () => import('@/views/Record/Product/Classification/index.vue'),
            meta: {
              title: '分类管理'
            }
          }
        ]
      },
      {
        path: 'franchiser',
        name: 'Franchiser',
        component: () => import('@/views/Record/Franchiser/Shop/index.vue'),
        meta: {
          title: '企业管理',
          icon: 'ci:user-02',
          alwaysShow: true
        },
        children: [
          {
            path: 'shop',
            name: 'Shop',
            component: () => import('@/views/Record/Franchiser/Shop/index.vue'),
            meta: {
              title: '公司列表'
            }
          }
        ]
      }
    ]
  },
  {
    path: '/document',
    component: Layout,
    redirect: '/document/purchase',
    name: 'Document',
    meta: {
      title: '单据管理',
      icon: 'ic:baseline-article',
      alwaysShow: true
    },
    children: [
      {
        path: 'purchase',
        name: 'Purchase',
        component: () => import('@/views/Document/Purchase/index.vue'),
        meta: {
          title: '采购单'
        }
      }
    ]
  },
  {
    path: '/finance',
    component: Layout,
    redirect: '/finance/bill',
    name: 'Finance',
    meta: {
      title: '财务管理',
      icon: 'iconoir:dollar-circle',
      alwaysShow: true
    },
    children: [
      {
        path: 'bill',
        name: 'Bill',
        component: () => import('@/views/Finance/Bill/index.vue'),
        meta: {
          title: '账单列表',
          btnPermission: []
        }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/role',
    name: 'System',
    meta: {
      title: '系统管理',
      icon: 'carbon:skill-level-advanced',
      alwaysShow: true
    },
    children: [
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/System/Role/index.vue'),
        meta: {
          title: '账号管理'
        }
      }
    ]
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
